package youleche.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import youleche.com.idao.IEmailConfig;
import youleche.com.model.MEmailConfig;
import youleche.com.model.MVehicleInfo;

/**
 * 邮件配置数据访问
 * @author Administrator
 *
 */
public class DEmailConfig implements IEmailConfig{
	/**
	 * 获取邮件配置集合
	 * @return 邮件配置集合
	 */
	@Override
	public List<MEmailConfig> getEmailConfigList(Connection con)  throws Exception{
		String sql = "SELECT " +
				"id,mailServerPort,userPassword,email" +
				",emailUserName,emailContent,emailTitle,isDelete,emailType" +
				",A1,A2,A3,mailServerHost " +
				"FROM emailconfig where isDelete=0";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = null;
		rs = ps.executeQuery();
		List<MEmailConfig> list = null;
		MEmailConfig config = null;
		if(rs != null){
			list = new LinkedList<MEmailConfig>();
			while(rs.next()) {
				config = new MEmailConfig();
				config.setId(rs.getString(1));
				config.setMailServerPort(rs.getString(2));
				config.setUserPassword(rs.getString(3));
				config.setEmail(rs.getString(4));
				config.setEmailUserName(rs.getString(5));
				config.setEmailContent(rs.getString(6));
				config.setEmailTitle(rs.getString(7));
				config.setIsDelete(rs.getInt(8));
				config.setEmailType(rs.getString(9));
				config.setA1(rs.getString(10));
				config.setA2(rs.getString(11));
				config.setA3(rs.getString(12));
				config.setMailServerHost(rs.getString(13));
				list.add(config);
			}
		}
		
		return list;
	}

	/**
	 * 添加邮件配置
	 * @return 返回添加的行数
	 */
	@Override
	public int addEmailConfig(MEmailConfig config, Connection con)  throws Exception{
		String sql = "INSERT emailconfig(id,mailServerHost,mailServerPort,userPassword,email,emailUserName,emailContent,emailTitle,isDelete,emailType,A1,A2,A3) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
		PreparedStatement pre = con.prepareStatement(sql);
		pre.setObject(1, config.getId());
		pre.setObject(2, config.getMailServerHost());
		pre.setObject(3, config.getMailServerPort());
		pre.setObject(4, config.getUserPassword());
		pre.setObject(5, config.getEmail());
		pre.setObject(6, config.getEmailUserName());
		pre.setObject(7, config.getEmailContent());
		pre.setObject(8, config.getEmailTitle());
		pre.setObject(9, config.getIsDelete());
		pre.setObject(10, config.getEmailType());
		pre.setObject(11, config.getA1());
		pre.setObject(12, config.getA2());
		pre.setObject(13, config.getA3());

		return pre.executeUpdate();
	}

	/**
	 * 禁用和启用
	 * @param con 数据库连接
	 * @param keyId keyId
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int udpateRemarkById(Connection con, String keyId, boolean isForbidden) throws Exception {
		String sql = "update emailconfig set a1="+ (isForbidden ? "0" : "1") +" where id='"+keyId+"' and isdelete = 0 limit 1";
		PreparedStatement pre = con.prepareStatement(sql);
		return pre.executeUpdate();
	}

	/**
	 * 删除配置信息
	 * @param con 连接
	 * @param keyId id
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int deleteConfigById(Connection con, String keyId) throws Exception {
		String sql = "update emailconfig set isdelete='1' where id='"+keyId+"' and isdelete = 0 limit 1";
		PreparedStatement pre = con.prepareStatement(sql);
		return pre.executeUpdate();
	}

}
